package com.Day1009;

import java.io.*;
import java.util.Scanner;

public class Task_1 {
    public static void main(String[] args) throws Exception {
        Scanner in =new Scanner(System.in);
//        String filePathR = "F:\\Java\\a.txt";
//        File fileReader = new File(filePathR);//读文件
//        FileInputStream fileInputStream = new FileInputStream(fileReader);
//        String filePathW = "F:\\Java\\b.txt";
//        File fileWriter = new File(filePathW);//写文件
//        FileOutputStream fileOutputStream = new FileOutputStream(fileWriter);
//        byte[] temp = new byte[1024];
//        StringBuffer stringBuffer = new StringBuffer();
//        while (fileInputStream.read(temp) != -1){
//            String str = new String(temp);
//            stringBuffer.append(str);
//        }
//        System.out.println(stringBuffer.toString());
//        System.out.print("请输入你的名字：");
//        String name = in.next();
//        System.out.print("请输入你的类型：");
//        String type = in.next();
//        System.out.print("请输入你主人的名字：");
//        String master_name = in.next();
//        stringBuffer.replace(8,14,name);
//        stringBuffer.replace(15,21,type);
//        stringBuffer.replace(22,31,master_name);
//        fileOutputStream.write(stringBuffer.toString().getBytes());
//        fileInputStream.close();
//        fileOutputStream.close();

        File file = new File("F:\\Java\\a.txt");
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        StringBuffer stringBuffer = new StringBuffer();
        String line = null;
        while ((line = bufferedReader.readLine()) != null){
            stringBuffer.append(line + "\n");
        }
        System.out.println(stringBuffer.toString());

        System.out.print("请输入你的名字：");
        String name = in.next();
        System.out.print("请输入你的类型：");
        String type = in.next();
        System.out.print("请输入你主人的名字：");
        String master_name = in.next();
        String result = stringBuffer.toString().replace("{name}",name).replace("{type}",type).replace("{master}",master_name);
        bufferedReader.close();
        fileReader.close();
        File fileW = new File("F:\\Java\\b.txt");
        FileWriter fileWriter = new FileWriter(fileW);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write(result);
        bufferedWriter.flush();//  刷新
        bufferedWriter.close();
        fileWriter.close();
    }
}
